package com.mcptt.db.op;

import java.util.List;
import java.util.Vector;

import com.cloudptt.api.product.log.Log;
import com.mcptt.db.DBProvider;
import com.mcptt.db.DbConst;
import com.mcptt.db.model.CameraInfo;
import com.mcptt.db.model.GroupInfo;
import com.mcptt.db.model.UserInfo;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

public class CameraDbOperator {

	public static String THIS_FILE = "CameraDbOperator";
	private Context ctx = null;

	public CameraDbOperator(Context ctx)
	{
		this.ctx = ctx;
	}
	
	public List<CameraInfo> getCameraList()
	{
		ContentResolver cr = ctx.getContentResolver();
		List<CameraInfo> list = new Vector<CameraInfo>();
		Cursor cursor = null;
		try 
		{
			cursor = cr.query(DbConst.CAMERA_URI,
					DBProvider.CAMERA_FULL_PROJECTION, null,
					null, DbConst._ID );

			if (cursor == null
					|| cursor.getCount() <= 0)
			{
				return list;
			}
			while (cursor.moveToNext()) 
			{  
				CameraInfo info = new CameraInfo(cursor);  
				list.add(info);
			} 
		} 
		catch (Exception e) 
		{
			// TODO: handle exception
			Log.debug(this.getClass(), "getCameraList e = " + e);
			e.printStackTrace();
		}
		finally
		{
			if(cursor != null)
			{
				cursor.close();
				cursor = null;
			}
		}

		return list;
	}
	
	
	public CameraInfo findCameraInfoByIp(String ip)
	{
		CameraInfo info = null;
		ContentResolver cr = ctx.getContentResolver();				
		String selection = DbConst.COL_Ip  + " =?";
		Cursor cursor = null;
		try 
		{
			cursor = cr.query(DbConst.CAMERA_URI, DBProvider.CAMERA_FULL_PROJECTION, selection, new String[]{ip}, null);	
			if(cursor !=null
					&& cursor.getCount() > 0)
			{			
				if(cursor.moveToFirst())
				{
					info = new CameraInfo(cursor);
				}
			}
		} 
		catch (Exception e) 
		{
			// TODO: handle exception
			Log.debug(this.getClass(), "findCameraInfoByIp e = " + e);
		}
		finally
		{
			if(cursor != null)
			{
				cursor.close();
				cursor = null;
			}
		}

		return info;
	}

	public void addCameraInfo(CameraInfo info)
	{
		Log.debug(this.getClass(), "addCameraInfo info = " + info);
		String ipStr = info.getIpStr();
		ContentResolver cr = ctx.getContentResolver();
		String selection = DbConst.COL_Ip + "=?";	

		Cursor cursor = null;
		try 
		{
			cursor = cr.query(DbConst.CAMERA_URI,
					DBProvider.CAMERA_FULL_PROJECTION, selection,
					new String[] {ipStr}, null);
			if (cursor == null
					|| cursor.getCount() <= 0)
			{
				Uri uri = cr.insert(DbConst.CAMERA_URI, info.getContentValues());
				Log.debug(this.getClass(), "addCameraInfo uri = " + uri);
			}
		} 
		catch (Exception e) 
		{
			Log.debug(this.getClass(), "addCameraInfo e = " + e);
			// TODO: handle exception
		}
		finally
		{
			if(cursor != null)
			{
				cursor.close();
				cursor = null;
			}
		}
	}
	
//	
//	
//	public void deleteAllGroupUser()
//	{
//		Log.debug(this.getClass(), "deleteAllGroupUser");
//		ContentResolver cr = ctx.getContentResolver();
//		//		cr.delete(DbConst.ADDRESS_URI, "1=1", null);
//		String selection = DbConst.COL_Type + "=?";
//		cr.delete(DbConst.ADDRESS_URI, selection,
//				new String[] { "1" });
//	}
	
	public void deleteCameraInfo(CameraInfo info)
	{
		Log.debug(this.getClass(), "deleteCameraInfo info = " + info);
		String ip = info.getIpStr();

		ContentResolver cr = ctx.getContentResolver();
		String selection = DbConst.COL_Ip + "=?";
		cr.delete(DbConst.CAMERA_URI, selection,
				new String[] { ip });
	}
	
}
